<!--
 * @Author: Y先森
 * @Date: 2023-04-14 09:53:32
 * @LastEditors: y && 250048395@qq.com
 * @LastEditTime: 2023-04-14 16:50:02
 * @FilePath: \y-website\src\libs\popup\index.vue
-->
<template>
  <div>
    <!-- teleport -->
    <teleport to="body">
      <!-- 蒙版 -->
      <transition name="fade">
        <div
          v-if="isVisable"
          class="h-screen w-screen fixed top-0 left-0 bg-zinc-900/80 z-40"
          @click="isVisable = false"
        ></div>
      </transition>
      <!-- 内容 -->
      <transition name="popup-left-up">
        <div v-if="isVisable" class="w-8/12 h-screen fixed bg-white left-0 top-0 z-50">
          <slot />
        </div>
      </transition>
    </teleport>
  </div>
</template>

<script setup>
import { watch } from 'vue'
import { useScrollLock, useVModel } from '@vueuse/core'

const props = defineProps({
  modelValue: {
    required: true,
    type: Boolean
  }
})

// 生产一个响应式数据,当isVisable发生改变是,会自动触发 emit 修改 modelValue的值
const isVisable = useVModel(props)
defineEmits(['update:modelValue'])

// ------ 滚动锁定 ------
const isLocked = useScrollLock(document.body)
watch(
  isVisable,
  (val) => {
    isLocked.value = val
  },
  { immediate: true }
)
</script>
<style scoped lang="scss">
// fade 展示动画
.fade-enter-active,
.fade-leave-active {
  transition: all 0.3s;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
// popup-left-up 展示动画
.popup-left-up-enter-active,
.popup-left-up-leave-active {
  transition: all 0.3s;
}
.popup-left-up-enter-from,
.popup-left-up-leave-to {
  transform: translateX(-100%);
}
</style>
